#include "StringSplit.hpp"

namespace se {

std::shared_ptr<std::vector<std::string>>
StringSplit::split(const std::string &stringToSplit, Encoding encoding) {
    switch (encoding)
    {
    case UTF8:
        return StringSplit::splitWithUtf8(stringToSplit);
    default:
        return nullptr;
    }
}

std::shared_ptr<std::vector<std::string>>
StringSplit::splitWithUtf8(const std::string &stringToSplit) {
    std::shared_ptr<std::vector<std::string>> characterList(new std::vector<std::string>);
    size_t length = 1;  // 单个字符的字节数

    for (size_t i = 0; i < stringToSplit.size(); i += length) {
        length = StringSplit::getUtf8CharacterLength(stringToSplit[i]);
        characterList->push_back(stringToSplit.substr(i, length));
    }
    return characterList;
}

size_t StringSplit::getUtf8CharacterLength(char character) {
    if ((character & 0x80) == 0) return 1;
    if ((character & 0xFC) == 0xFC) return 6;
    if ((character & 0xF8) == 0xF8) return 5;
    if ((character & 0xF0) == 0xF0) return 4;
    if ((character & 0xE0) == 0xE0) return 3;
    if ((character & 0xC0) == 0xC0) return 2;
    return 1;
}

}  // namespace se